/**
 * Created by lvlq on 16/4/23.
 */


$(function () {
    FastClick.attach(document.body);
    loading(function () {
        router.run();
    });

    $("#home").on("click", ".begin_btn", function () {
        router.href("/mine");
    }).on("click", ".rule_btn", function () {
        router.href("/rule");
    });

    $("#mine").on("click", ".btn", function () {
        if ($("#mine .dialog.dialog_share").hasClass("active")) return;
        if ($("#mine .dialog.dialog_phone_commit").hasClass("active")) return;
        checkUserPhone(function (flag) {
            if (flag) {
                $("#mine .dialog.dialog_share").addClass("active").fadeIn();
            } else {
                $("#mine .dialog.dialog_phone_commit").addClass("active").fadeIn();
            }
        });

    }).on("click", ".dialog_phone_commit .save_btn", function () {
        var phone = $(".dialog_phone_commit input").val();
        phone = phone.trim();
        if (!(/^1[3|4|5|8|7]\d{9}$/.test(phone))) {
            return alert("手机号格式不正确", 3);
        }
        commitPhone(phone, function () {
            $(".dialog_phone_commit").removeClass("active").fadeOut(function () {
                $(this).hide();
            });
            $("#mine .dialog.dialog_share").addClass("active").fadeIn();
        });

    }).on("click", ".dialog_phone_commit .close", function () {
        $(".dialog_phone_commit").removeClass("active").fadeOut(function () {
            $(this).hide();
        });
    }).on("click", ".bottom_right .rule", function () {
        router.href("/rule");
    });

    $("#share .prize_status .num .add").on(aniend, function () {
        $(this).removeClass("active")
    });

    $("#share").on("click", ".btn .help_friend", function () {
        g_pid = null;
        getShareNum(function () {
            if (g_pid) {
                clickAdd(g_pid, function (data) {
                    $("#share .prize_status .i-" + g_pid + " .num .count").html(data.already_num);
                    $("#share .prize_status .i-" + g_pid + " .num .sum").html(data.num);
                    alert1("+1", 2);
                    // $("#share .prize_status .i-" + g_pid + " .num .add").addClass("active");
                });
            } else {
                alert("好友的打气数已经满啦~~");
            }
        });
        // if ($("#share .dialog.dialog_share").hasClass("active")) return;
        // $("#share .dialog.dialog_share").addClass("active").fadeIn();
    }).on("click", ".btn .do_self", function () {
        router.href("/home");
    }).on("click", ".status .prize_status .item", function () {
        // var pid = $(this).data("pid");
        // var self = this;
        // clickAdd(pid, function (data) {
        //     $(self).find(".num .count").html(data.already_num);
        //     $(self).find(".num .sum").html(data.num);
        // });
    }).on("click", ".bottom_right .rule", function () {
        router.href("/rule");
    });


    $("#home .title").on(aniend, function () {
        $("#home .bottom_left").addClass("fadeInLeftBig");
        $("#home .bottom_right").addClass("fadeInRightBig");
    });

    $("#home .bottom_right").on(aniend, function () {
        $("#home .bottom_lt").addClass("fadeInUpBig");
    });

    $("#home .bottom_lt").on(aniend, function () {
        $("#home .ball").addClass("fadeInDownBig");
    });

    $("#home .ball").on(aniend, function () {
        $("#home .begin_btn").addClass("fadeIn");
        $("#home .rule_btn").addClass("fadeIn");
    });

    $("#mine .dialog.dialog_share").on("click", function () {
        $(this).fadeOut(function () {
            $(this).removeClass("active").hide();
        });
    })
});

$(function () {
    var dw = $(window).width();
    var dh = $(window).height();

    var ch = (1008 * dw) / 640;


    if (ch > dh) {
        var ro = dh / ch;
        $("#home,#mine,#share").css({
            "font-size": ro + "rem"
        });
    }
});

var loading = function (cb) {
    const IMG_SRC = "/Public/index/image/";
    var srcs = [
        "rule.jpg",
        "bottom_left_tr.png",
        "bottom_right_tr.png",
        "bottom_lt.png",
        "home_ball.png",
        "home_begin_btn.png",
        "home_bg.png",
        "home_rule_btn.png",
        "home_wang.png",
        "logo.png",
        "rule.jpg",
        "title.png",
        "title_little.png",
        "mine_beng_icon.png",
        "mine_bg.jpg",
        "mine_btn.png",
        "mine_prize.png",
        "mine_close.png",
        "mine_phone_save_btn.png",
        "share-btn.png",
        "left_person.png",
        "dialog_guanzhu.png"
    ];

    var load = function (srcs) {
        if (srcs.length <= 0) {
            return cb && cb();
        }
        var src = srcs.pop();
        src = IMG_SRC + src;
        load_img(src, function () {
            load(srcs);
        });
    };

    load(srcs);
};


var load_img = function (src, cb) {
    var img = new Image();
    img.src = src;
    img.onload = img.onerror = function () {
        cb && cb();
    }
};


var getShareContent = function (type, openid) {
    $.ajax({
        url: "/index/pumpUp/getShareContent",
        type: "POST",
        data: {
            type: type,
            q_openid: openid
        },
        dataType: "json",
        success: function (data) {
            if (!data.errno) {
                setShare(data.data);
            }
        },
        error: function () {
            alert("网络错误");
        }
    })
};

var setShare = function (data) {
    var shareData = {
        title: data.title,
        link: data.link,
        imgUrl: data.img,
        desc: data.desc,
        success: shareSuccess
    };

    var qshareData = {
        title: data.qtitle,
        link: data.link,
        imgUrl: data.img,
        success: shareSuccess
    };

    wx.onMenuShareTimeline(qshareData);
    wx.onMenuShareAppMessage(shareData);
};

var getMineShareContent = function () {
    getShareContent(TYPE_MINE, null);
};

var getFriendShareContent = function () {
    var q_openid = getQueryString("q_openid");
    getShareContent(TYPE_SHARE, q_openid);
};

var clickAdd = function (pid, cb) {
    var q_openid = getQueryString("q_openid");
    $.ajax({
        url: "/index/pumpUp/clickAdd",
        type: "POST",
        data: {
            pid: pid,
            q_openid: q_openid
        },
        dataType: "json",
        success: function (data) {
            if (!data.errno) {
                cb && cb(data.data);
            } else {
                alert(data.errdesc);
            }
        },
        error: function () {
            alert("网络错误");
        }
    })
};

var commitPhone = function (phone, cb) {
    $.ajax({
        url: "/index/pumpUp/commitPhone",
        type: "POST",
        data: {
            phone: phone
        },
        dataType: "json",
        success: function (data) {
            if (!data.errno) {
                cb && cb();
            } else {
                alert(data.errdesc);
            }
        },
        error: function () {
            alert("网络错误");
        }
    })
};

var checkUserPhone = function (cb) {
    $.ajax({
        url: "/index/pumpUp/checkUserPhone",
        type: "POST",
        data: {},
        dataType: "json",
        success: function (data) {
            if (!data.errno) {
                cb && cb(true);
            } else {
                cb && cb(false);
            }
        },
        error: function () {
            alert("网络错误");
        }
    })
};


